package com.cloud.pole.alipay;

import java.io.StringWriter;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.Cipher;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.ASN1Object;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.asn1.pkcs.RSAPrivateKeyStructure;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.util.io.pem.PemObject;
import org.bouncycastle.util.io.pem.PemWriter;
import org.bouncycastle.asn1.ASN1Primitive;

public class LzyanRSAUtilTest {

	public LzyanRSAUtilTest() {
		// TODO Auto-generated constructor stub
	}

	public static final String KEY_ALGORITHM = "RSA";
	private static final String PUBLIC_KEY = "rsa_public_key";
	private static final String PRIVATE_KEY = "rsa_private_key";
	private static final String ENCODING = "UTF-8";
	
	//format PKCS#1 to PKCS#8
    public static String formatPkcs1ToPkcs8(String rawKey) throws Exception {
        String result = null;
        //extract valid key content
        String validKey = rawKey;//RsaPemUtil.extractFromPem(rawKey); // pem文件多行合并为一行
//        if(!Strings.isNullOrEmpty(validKey))
        if (!StringUtils.isEmpty(validKey))
        {
            //将BASE64编码的私钥字符串进行解码
            byte[] encodeByte = Base64.decodeBase64(validKey);
            
            AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier(PKCSObjectIdentifiers.pkcs8ShroudedKeyBag);    //PKCSObjectIdentifiers.pkcs8ShroudedKeyBag        
//            ASN1Object asn1Object = ASN1Object.fromByteArray(encodeByte);
            ASN1Object asn1Object = ASN1ObjectIdentifier.fromByteArray(encodeByte);
            PrivateKeyInfo privKeyInfo = new PrivateKeyInfo(algorithmIdentifier, asn1Object);            
            byte[] pkcs8Bytes = privKeyInfo.getEncoded();
            
            String type = "PRIVATE KEY";
//            result = format2PemString(type, pkcs8Bytes); // 格式化为pem多行格式输出
            return Base64.encodeBase64String(pkcs8Bytes); // 直接一行字符串输出
            
        }
        return result;
    }

	// === Testing ===
	public static void main(String[] args) {
		try {
	       String privateKey = "MIIEogIBAAKCAQEAm5D0xpGLq+XC81NLNafskxq1fuopfnJRRI3I6i7crLZNc/XJZhHoL+hfBiGNRxDFJB6/+734yUGcS8tJPU9skdSk7OOQROgCTXYdWNFB1mTW5umflH39ycR1TchCtQ+e2Ovv9Z6ztx8aCseiL0dHPr2vU6/QcW/pol9fGm//9wVNvYvgnxcTDfZ7/Yt/eAa0CmMkiOAiIlK5EnQbNUdPsm5AIjnS0wUl9Ub2EXSBvmBiY6TlfcuB6vkV+NyldqKABatcTzIQ3N9v36x3JzVBVStUcD83opqAT58EY5he3pNUMlHmk4inUDWGX/633HHdkIYC33DOLVxf7Rqq4MojKwIDAQABAoIBAF8IHy+KS9t3oS8JrAEcEv4OK99a5nwzE4ggYSMF+5KaTxhOhniO6dsSRbI2HtrR2Mxm6Hq9o1Q2e2clyRjmAvnW/+UiYshlPrrZM7ZFJsX0lf87qluI19dA5zPb6Tw4TZ5QCydtinWMT1lUeLQPA+vE7mzeEySo6/1f63X4A1J/eupCsqtMUINRfIJSHwAYDInpO0I+aXApsHuhfWXOkb9lYKYYmT719M3PuayG5No7O3oHbpvJbjRg8ZrO4xu5NMhqVuvaFGoPt+RME+8pWyCVBRPZjRlOoG92t7FAiDRqWmcbMV26YvK54RNofk5kwoN8D/TG/Ke0DFXU8uKn9wkCgYEA3EuTaQ+QFhlkvH7qMdrFSQXEA7ec6iSVcHkN+/fNYHphK54EJGlXiTshs0JK7vobw1JfkbTpIjMDqyoksMDryWDfAI6JZgwYOLIByI931LloWihm03mKo33URYHQNkpnEywy7ONIo7hXb3/MiBtDLKVokP3rPiMneSgubi0mHn8CgYEAtMeqGqMerwxbRNAVmkGZCvy08Dxw+QPw7vd/yFg8srV7ofoR3TJ0bbXnFcc5KrWwMkCQfUu4Th9WB8doGMHwFLBNEBYbdtwvWUXz71C8D3YnuuZvbw6v0fye55N+ZWi7a2Pqrcs13+R+J8Y7KoZT7sUaXccocCwRIYJN19bCfVUCgYBIy1ByrwnVw55eCzIoNzpp3tXexJW9aUlYWSgd0syyzL2/Q//qaETcvAVuzsEkJtF1wYqDA1zmSCU2Mj7diWJgsPn/nRoZ9nUqhue/9miKipnI8QDJcKg3TiUOss6yofNdioVpcdFXCFscJQbZEuknn6DmCIHrjoJErtpo3dJy0QKBgE5sJ0Cy7oalZ5psYlKujkOT6LImpiOAMC6aM1an4Hzt+ZoEr2czkgpd1Mc/y27Zrnu+Qm/4PxW0xk9Nk7zZtNpkuKl3zaY8UlX54J46ebqDWSzc9g5JYsBHRG/VLgmz60nk6FNISjC8IHYEqnoCF7sGyi9QTByNIUlWC4rMYUaZAoGAeM2qFMtykCCI+uuYE5+qToDQqmtbcNEpG06xNxcwRhOZD9/pthGqiMxs9WwwkgWTlB+OvYp3qq1DRY3j2amZ12tfJeGQpq8j4FUvlbGNONAHYW1ozZAPrNnzkEhnTGDqld0YQ8sd37TgasEmnAlSj+kqhCsLQ2grbmN+ypaw3nA=";
	       String str = formatPkcs1ToPkcs8(privateKey);
	       System.out.println(str);		
	       
	       String strMsg = "";
	       System.out.println("ddddddddd");	

	       strMsg += "MIIEtDCCA5ygAwIBAgIQICAGIo0W5ScQUzQQDYvWYjANBgkqhkiG9w0BAQsFADCBgjELMAkGA1UE";
	       strMsg += "BhMCQ04xFjAUBgNVBAoMDUFudCBGaW5hbmNpYWwxIDAeBgNVBAsMF0NlcnRpZmljYXRpb24gQXV0";
	       strMsg += "aG9yaXR5MTkwNwYDVQQDDDBBbnQgRmluYW5jaWFsIENlcnRpZmljYXRpb24gQXV0aG9yaXR5IENs";
	       strMsg += "YXNzIDEgUjEwHhcNMjAwNjIyMDk1MDI2WhcNMjIwNjIyMDk1MDI2WjB8MQswCQYDVQQGEwJDTjEw";
	       strMsg += "MC4GA1UECgwn5rmW5Y2X6YeR6JyC5pif6YCa5L+h6K6+5aSH5pyJ6ZmQ5YWs5Y+4MQ8wDQYDVQQL";
	       strMsg += "DAZBbGlwYXkxKjAoBgNVBAMMITIwODg3MzE4NTkzNDQxMjMtMjAyMTAwMTE0NDYwMzQxMDCCASIw";
	       strMsg += "DQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAJuQ9MaRi6vlwvNTSzWn7JMatX7qKX5yUUSNyOou";
	       strMsg += "3Ky2TXP1yWYR6C/oXwYhjUcQxSQev/u9+MlBnEvLST1PbJHUpOzjkEToAk12HVjRQdZk1ubpn5R9";
	       strMsg += "/cnEdU3IQrUPntjr7/Wes7cfGgrHoi9HRz69r1Ov0HFv6aJfXxpv//cFTb2L4J8XEw32e/2Lf3gG";
	       strMsg += "tApjJIjgIiJSuRJ0GzVHT7JuQCI50tMFJfVG9hF0gb5gYmOk5X3Lger5FfjcpXaigAWrXE8yENzf";
	       strMsg += "b9+sdyc1QVUrVHA/N6KagE+fBGOYXt6TVDJR5pOIp1A1hl/+t9xx3ZCGAt9wzi1cX+0aquDKIysC";
	       strMsg += "AwEAAaOCASkwggElMB8GA1UdIwQYMBaAFHEH4gRhFuTl8mXrMQ/J4PQ8mtWRMB0GA1UdDgQWBBQz";
	       strMsg += "J0irnb7SO6rf37IUvNh41hWIJjBABgNVHSAEOTA3MDUGB2CBHAFuAQEwKjAoBggrBgEFBQcCARYc";
	       strMsg += "aHR0cDovL2NhLmFsaXBheS5jb20vY3BzLnBkZjAOBgNVHQ8BAf8EBAMCBsAwLwYDVR0fBCgwJjAk";
	       strMsg += "oCKgIIYeaHR0cDovL2NhLmFsaXBheS5jb20vY3JsNDIuY3JsMGAGCCsGAQUFBwEBBFQwUjAoBggr";
	       strMsg += "BgEFBQcwAoYcaHR0cDovL2NhLmFsaXBheS5jb20vY2E2LmNlcjAmBggrBgEFBQcwAYYaaHR0cDov";
	       strMsg += "L2NhLmFsaXBheS5jb206ODM0MC8wDQYJKoZIhvcNAQELBQADggEBAHgaubbX7/ElLBNUS3zyb8BT";
	       strMsg += "m5F6DhNYsAbFkHqBPJn1CNVNXYPDaZMAMBiBXFOwzz7pVVMmx9PWWXzzwIPZ5qe/LapttjNJM9Kf";
	       strMsg += "DVZCysPIeAzWEhxcT9nWhGPZvUnFsvzUprdL8EJOA50n3lk2IQ5xWkob4QTwqg6wSqF4dVldFs5A";
	       strMsg += "XwyxE2yt91gb33bdVoyF4AIt51DEsOT5VBPLusX4aTQaz2/rDlaGvSNEXxlIFQkVdtEfcdn9aMfD";
	       strMsg += "qOkMJ1yjZ6vI/AFdOQQlZyrCZlQopLLpJRK6lQYGNJe3lZ6r4qCDmYrrRLTaxZEQMSQWcbOv/QlL";
	       strMsg += "9QhPpLTyzGdy/rA=";   
	       String str1 = formatPkcs1ToPkcs8(strMsg);
	       System.out.println(str1);	
		} catch(Exception ex) {ex.printStackTrace();}
	       
	}
}